package components.contorls.superClass
{
    import components.core.UIComponent;
    
    import flash.display.DisplayObject;
    import flash.display.InteractiveObject;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    /**
     *可选择对象
     * @author zn
     *
     */
    public class BaseSelectedGroup extends UIComponent
    {
        private var _groupNameList:Array = [];

        private var _groupList:Vector.<Sprite> = new Vector.<Sprite>();

        private var _selectedObject:Sprite;

        private var _selectedObjectName:String;

        public function BaseSelectedGroup()
        {
            super();
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/ /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();
        }

        protected override function measureSizes():Boolean
        {
            return super.measureSizes();
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        [Inspectable(defaultValue="")]
        public function get selectedObjectName():String
        {
            return _selectedObjectName;
        }

        public function set selectedObjectName(value:String):void
        {
            if (parent)
            {
                _selectedObjectName = value;
                selectedObject(Sprite(parent.getChildByName(_selectedObjectName)));
            }
        }


        [Inspectable(defaultValue="", type="Array")]
        public function get groupNameList():Array
        {
            return _groupNameList;
        }

        public function set groupNameList(value:Array):void
        {
            var interactiveObject:InteractiveObject;
            if (_groupNameList)
            {
                for each (interactiveObject in _groupList)
                {
                    interactiveObject.removeEventListener(MouseEvent.CLICK, interactiveObject_clickHandler);
                }
            }

            removeSelectedState();
            _groupList = new Vector.<Sprite>();

            if (parent)
            {
                _groupNameList = value;

                for (var i:int = 0; i < _groupNameList.length; i++)
                {
                    var elementName:String = _groupNameList[i] as String;

                    interactiveObject = parent.getChildByName(elementName) as InteractiveObject;
                    interactiveObject.addEventListener(MouseEvent.CLICK, interactiveObject_clickHandler, false, 0, true);
                    _groupList.push(interactiveObject);
                }
            }
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        private function interactiveObject_clickHandler(event:MouseEvent):void
        {
            selectedObject(Sprite(event.currentTarget));
        }

        private function selectedObject(value:Sprite):void
        {
            removeSelectedState();

            _selectedObject = value;
            if (_selectedObject)
            {
                if (_selectedObject.hasOwnProperty("selected"))
                    _selectedObject["selected"] = true;
            }
        }

        private function removeSelectedState():void
        {
            if (_selectedObject)
            {
                if (_selectedObject.hasOwnProperty("selected"))
                    _selectedObject["selected"] = false;
            }

        }

        public function get selectedItem():*
        {
            if (_selectedObject.hasOwnProperty("value"))
                return _selectedObject["value"];

            return null;
        }
		
		protected override function frameConstructedHandler(event:Event):void
		{
			super.frameConstructedHandler(event);
			
			visible=false;
		}

    }
}